package com.alen.sun.controller;

import com.alen.sun.bean.Category;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.CategoryService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/categories")
@CrossOrigin
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result addCategory(@RequestBody Category category) {
        log.info("add被调用");
        Boolean flag = categoryService.addCategory(category);
        if(flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateCategory(@RequestBody Category category){
        Boolean flag = categoryService.updateCategory(category);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("修改分类信息失败");
        }
    }

    /**
     * 删除分类信息
     * @param category
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteCategory(@RequestBody Category category){
        Boolean flag = categoryService.deleteCategory(category);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("删除分类信息失败");
        }
    }

    /**
     * 分页查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams){
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = categoryService.page(page, pageSize);
        log.info("Page得到了使用");
        log.info("{}",pageBean);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = categoryService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }
    }

    /**
     * 获取分类数据
     * @return
     */
    @GetMapping
    @ResponseBody
    public Result getCategories(){
        List<Category> categories = categoryService.list();
        if (categories!=null){
            return Result.success(categories);
        }else
        {
            return Result.error("暂无数据");
        }
    }

    /**
     * 根据关键词获取数据
     * @return
     */
    @GetMapping("/word")
    @ResponseBody
    public Result word(@RequestParam(required = false) String searchName){
        List<Category> categories = categoryService.word(searchName);
        if (categories!=null){
            return Result.success(categories);
        }else {
            return Result.error("暂无数据");
        }
    }
}
